/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.BrandNameDAO;
import model.dao.CatalogueDAO;
import model.dao.FilterCellDAO;
import model.dao.FilterCpuDAO;
import model.dao.FilterCurrentCpuDAO;
import model.dao.FilterDisplayDAO;
import model.dao.FilterHddDAO;
import model.dao.FilterRamDAO;
import model.dao.MenuDAO;
import model.dao.ProductDAO;
import model.dao.SlideDAO;
import model.pojo.BrandName;
import model.pojo.Catalogue;
import model.pojo.FilterCell;
import model.pojo.FilterCpu;
import model.pojo.FilterCurrentCpu;
import model.pojo.FilterDisplay;
import model.pojo.FilterHdd;
import model.pojo.FilterRam;
import model.pojo.Product;
import model.pojo.Slide;

/**
 *
 * @author Ti Oc
 */
@WebServlet(name = "ProductController", urlPatterns = {"/ProductController"})
public class ProductController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            String action = request.getParameter("Action");
            String name = request.getParameter("Name");
            if(action != null)
            {
                if(action.equals("View"))
                {
                    //Lay danh sach slide
                    ArrayList<Slide> slide = SlideDAO.getSlide();
                    request.setAttribute("Slide", slide);
                    
                    //Lấy danh sách menu trả về trang home
                    ArrayList<model.pojo.Menu> menu = MenuDAO.getMenu();
                    request.setAttribute("Menu", menu);
                    
                    //Lấy danh sách brandname
                    ArrayList<BrandName> brandName = BrandNameDAO.getBrandName();
                    request.setAttribute("BrandName", brandName);
                    
                    //Lọc sản phẩm theo catalogue
                    //Lấy danh sách catalogue
                    ArrayList<Catalogue> catalogue = CatalogueDAO.getCatalogue();
                    request.setAttribute("Catalogue", catalogue);
                    
                    //Lấy danh sách lọc sản phẩm theo dòng Cpu
                    ArrayList<FilterCurrentCpu> filterCurrentCpu = FilterCurrentCpuDAO.getFilterCurrentCpu();
                    request.setAttribute("FilterCurrentCpu", filterCurrentCpu);
                    
                    //Lấy danh sách lọc sản phẩm theo Cpu
                    ArrayList<FilterCpu> filterCpu = FilterCpuDAO.getFilterCpu();
                    request.setAttribute("FilterCpu", filterCpu);
                    
                    //Lấy danh sách lọc sản phẩm theo ram
                    ArrayList<FilterRam> filterRam = FilterRamDAO.getFilterRam();
                    request.setAttribute("FilterRam", filterRam);
                    
                    //Lấy danh sách lọc sản phẩm theo hdd
                    ArrayList<FilterHdd> filterHdd = FilterHddDAO.getFilterhdd();
                    request.setAttribute("FilterHdd", filterHdd);
                    
                    //Lấy danh sách lọc sản phẩm theo Display
                    ArrayList<FilterDisplay> filterDisplay = FilterDisplayDAO.getFilterDisplay();
                    request.setAttribute("FilterDisplay", filterDisplay);
                    
                    //Lấy danh sách lọc sản phẩm theo Cell
                    ArrayList<FilterCell> filterCell = FilterCellDAO.getFilterCell();
                    request.setAttribute("FilterCell", filterCell);
                    
                    //Loc san pham theo...
                    ArrayList<Product> productFilter = new ArrayList<Product>(); 
                    String nameFilter = "";
                    String parameter = "";
                    int soTrang = 0;
                    //Phan trang san pham
                    int trang = 1;
                    if(request.getParameter("Trang") != null)
                    {
                        trang = Integer.parseInt(request.getParameter("Trang"));
                    }
                    if("CurrentCPU".equals(name))
                    {
                        String currentCPU = request.getParameter("Parameter");
                        parameter = currentCPU;
                        name = "CurrentCPU";
                        parameter = currentCPU;
                        productFilter = ProductDAO.FilterCurrentCpu(currentCPU, trang);
                        //Tính số lượng trang
                        soTrang = ProductDAO.tinhSoLuongTrangCurrentCpu(currentCPU);
                        request.setAttribute("TrangTruocSau", trang); //Trang truoc va sau
                    }
                    else if("SpeedCPU".equals(name))
                    {
                        double sizeCpu = Double.valueOf(request.getParameter("Parameter"));
                        nameFilter = ">= " + String.valueOf(sizeCpu) + " Ghz";
                        name = "SpeedCPU";
                        parameter = String.valueOf(sizeCpu);
                        productFilter = ProductDAO.FilterSizeCpu(sizeCpu, trang);
                         //Tính số lượng trang
                        soTrang = ProductDAO.tinhSoLuongTrangSpeedCpu(sizeCpu);
                        request.setAttribute("TrangTruocSau", trang); //Trang truoc va sau
                    }
                    else if("RAM".equals(name))
                    {
                        int sizeRam = Integer.valueOf(request.getParameter("Parameter"));
                        nameFilter = ">= " + String.valueOf(sizeRam) + " GB";
                        name = "RAM";
                        parameter = String.valueOf(sizeRam);
                        productFilter = ProductDAO.FilterRam(sizeRam, trang);
                         //Tính số lượng trang
                        soTrang = ProductDAO.tinhSoLuongTrangRam(sizeRam);
                        request.setAttribute("TrangTruocSau", trang); //Trang truoc va sau
                    }
                    else if("HDD".equals(name))
                    {
                        int sizeHdd = Integer.valueOf(request.getParameter("Parameter"));
                        nameFilter = ">= " + String.valueOf(sizeHdd) + " GB";
                        name = "HDD";
                        parameter = String.valueOf(sizeHdd);
                        productFilter = ProductDAO.FilterHdd(sizeHdd, trang);
                        //Tính số lượng trang
                        soTrang = ProductDAO.tinhSoLuongTrangHdd(sizeHdd);
                        request.setAttribute("TrangTruocSau", trang); //Trang truoc va sau
                    }
                    else if("Display".equals(name))
                        
                    {
                        double sizeDisplay = Double.valueOf(request.getParameter("Parameter"));
                        nameFilter = ">= " + String.valueOf(sizeDisplay) + " inch";
                        name = "Display";
                        parameter = String.valueOf(sizeDisplay);
                        productFilter = ProductDAO.FilterDispaly(sizeDisplay, trang);
                        //Tính số lượng trang
                        soTrang = ProductDAO.tinhSoLuongTrangDisplay(sizeDisplay);
                        request.setAttribute("TrangTruocSau", trang); //Trang truoc va sau
                    }
                    else if("Battery".equals(name))
                    {
                        int sizeCell = Integer.valueOf(request.getParameter("Parameter"));
                        nameFilter = String.valueOf(sizeCell) + " cell";
                        name = "Battery";
                        parameter = String.valueOf(sizeCell);
                        productFilter = ProductDAO.FilterBattery(sizeCell, trang);
                        //Tính số lượng trang
                        soTrang = ProductDAO.tinhSoLuongTrangBattery(sizeCell);
                        request.setAttribute("TrangTruocSau", trang); //Trang truoc va sau
                    }
                    request.setAttribute("NameFilter", nameFilter);
                    request.setAttribute("ProductFilter", productFilter);
                    request.setAttribute("Name", name);
                    request.setAttribute("Parameter", parameter);
                    request.setAttribute("SoTrang", soTrang);

                    request.getRequestDispatcher("/Product.jsp").forward(request, response);
                }
            }
        } 
        finally 
        {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
